#include <iostream>
#include <fstream>
#include <direct.h>
#include <windows.h>
#include <vector>
#include <algorithm>
#include <CXX/Objects.hxx>

using namespace std;

std::string genTemplate(std::vector<std::string> clzs)
{
	/*std::string tml =
		"DEF_WORKBENDATA($Workbench)\n"
		"void $Workbench::Init()\n"
		"{\n"
		"\tMenuText = \"$\";\n"
		"\tToolTip = \"$ workbench\";\n"
		"\tIcon = App::GetApplication().getResourceDir() + \"/Mod/$/Resources/icons/$Workbench.svg\";\n"
		"}\n"
		"void $Workbench::Initialize()\n"
		"{\n"
		"\tPyImport_ImportModule(\"$Gui\");\n"
		"}\n"
		"std::string $Workbench::GetClassName() const\n"
		"{\n"
		"\treturn \"$Gui::Workbench\";\n"
		"}\n";*/
	std::string tml=R"(
DEF_WORKBENDATA($Workbench)
void $Workbench::Init()
{
	MenuText = "$";
	ToolTip = "$ workbench";
	Icon = App::GetApplication().getResourceDir() + "/Mod/$/Resources/icons/$Workbench.svg";
}
void $Workbench::Initialize()
{
	PyImport_ImportModule("$Gui");
}
std::string $Workbench::GetClassName() const
{
	return "$Gui::Workbench";
}
)";
	std::string res;
	for (auto clz : clzs) {
		std::string tmp;
		for (char c : tml) {
			if (c == '$')tmp += clz;
			else tmp += c;
		}
		res += tmp;
	}
	return res;
}
std::string genTemplatep(std::vector<std::string> clzs)
{
	std::string tml = R"(
DEF_WORKBENDATA($Workbench)
void $Workbench::Init()
{
	MenuText = "$";
	ToolTip = "$ workbench";
	Icon = App::GetApplication().getResourceDir() + "/Mod/$/Resources/icons/$Workbench.svg";
}
void $Workbench::Initialize()
{
	PyImport_ImportModule("$Gui");
}
std::string $Workbench::GetClassName() const
{
	return "Gui::PythonWorkbench";
}
)";
	std::string res;
	for (auto clz : clzs) {
		std::string tmp;
		for (char c : tml) {
			if (c == '$')tmp += clz;
			else tmp += c;
		}
		res += tmp;
	}
	return res;
}
std::string genAdd(std::vector<std::string> clz1, std::vector<std::string> clz2)
{
	std::string head = 
		"void WorkbenchDataPyManager::load()\n"
		"{\n"
		"\tWorkbenchData* _ws[] = {\n";

	std::string tail = 
		"\t};\n"
		"\tfor (auto w : _ws) {\n"
		"\t\tw->Init();\n"
		"\t\taddWorkbenchData(w);\n"
		"\t}\n"
		"}\n";
	std::string res = head;
	for (auto clz : clz1)res += "\t\tnew " + clz + "Workbench,\n";
	for (auto clz : clz2)res += "\t\tnew " + clz + "Workbench,\n";
	return res + tail;
}
int main(int args, char* argc[])
{
	std::vector<std::string> v = {"Drawing","Fem","Image","Inspection","Mesh","Points","Raytracing","ReverseEngineering","Robot",
		"Sketcher","Spreadsheet","Start","Surface","TechDraw","Test","Web","Inspection","Image","PartDesign","Part","MyModule"};
	std::vector<std::string> f = { "Draft","Arch","OpenSCAD" };
	std::string res=genTemplate(v);
	res += genTemplatep(f);
	res += genAdd(v, f);
	std::ofstream of("WorkbenchDataPyModImpl.txt");
	of << res;

	return 0;
}